<template>
  <div class="tabs">
    <el-tag
      size="small"
      v-for="(item, index) in tags"
      :key="item.name"
      :closable="item.name !== 'home'"
      :effect="item.name === $route.name ? 'dark' : 'plain'"
      :disable-transitions="false"
      @click="changeMenu(item)"
      @close="handleClose(item, index)"
    >
      {{ item.label }}
    </el-tag>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";

export default {
  name: "CommonTags",
  data() {
    return {};
  },
  computed: {
    ...mapState({
      tags: (state) => state.tab.tabsList,
    }),
  },
  methods: {
    ...mapMutations({
        close: 'closeTag'
    }),
    changeMenu(item) {
      this.$router.push({
        name: item.name,
      });
    },
    handleClose(tag, index) {
      const length = this.tags.length - 1
      this.close(tag)
      if (tag.name !== this.$route.name) {
        return;
      } else {
        if (index === length) {
          this.$router.push({ name: this.tags[length - 1].name });
        }else{
          this.$router.push({ name: this.tags[index].name });

        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
.tabs {
  display: flex;
  justify-content: flex-start;

  .el-tag {
    margin: 10px;
  }
}
</style>
